package com.yuan.web.common.tuguan;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;

public class Baidu {

	private static final String referer = "www.youmo365.com/ads.html";

	private static final int jiange = 1;

	private static final int times = 2;

	public static void main(String[] args) {

		for (int pg = 0; pg < 76; ++pg) {

			System.out.println("开始取得用户,PageSize:" + pg);
			ArrayList<String> users = Baidu.getUser(pg);
			if (users == null || users.isEmpty()) {
				System.out.println("查看是否为网络异常");
				return;
			}
			System.out.println("取得用户数量:" + users.size());

			for (String string : users) {

				for (int j = 0; j < times; j++) {
					System.out.println("开始：" + string + "第" + j + "次");
					String temp = Baidu.sendBaidu(string, referer, jiange);
					System.out.println("结束：" + string + "," + temp + "第" + j
							+ "次");
				}
			}
		}
	}

	public static synchronized String sendBaidu(String url, String referer,
			int jiange) {
		try {
			if (send(url, referer)) {
				return "成功";
			}
			return "失败";
		} catch (UnknownHostException e) {
		} catch (IOException e) {
		} finally {
			try {
				Thread.sleep(jiange * 1000);
			} catch (InterruptedException localInterruptedException2) {
			}
		}

		return "失败";
	}

	public static synchronized boolean send(String url, String referer)
			throws UnknownHostException, IOException {
		Socket socket = new Socket("hi.baidu.com", 80);
		StringBuffer sb = new StringBuffer("GET /" + url + " HTTP/1.1");
		sb.append("\r\n");
		sb.append("Host: hi.baidu.com");
		sb.append("\r\n");
		sb.append("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8 GTB6");
		sb.append("\r\n");
		sb.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		sb.append("\r\n");
		sb.append("Accept-Language: zh,en;q=0.8,zh-cn;q=0.5,en-us;q=0.3");
		sb.append("\r\n");
		sb.append("Accept-Encoding: gzip,deflate");
		sb.append("\r\n");
		sb.append("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
		sb.append("\r\n");
		sb.append("Referer: http://" + referer);
		sb.append("\r\n");
		sb.append("\r\n");

		OutputStream socketOut = socket.getOutputStream();
		socketOut.write(sb.toString().getBytes());
		socket.shutdownOutput();

		InputStream socketIn = socket.getInputStream();
		DataInputStream dis = new DataInputStream(socketIn);
		String str = dis.readLine();

		if (dis != null) {
			dis.close();
		}
		if (socket != null) {
			socket.close();
		}
		if (str == null) {
			return false;
		}

		return (str.endsWith("HTTP/1.1 200 OK"));
	}

	public static synchronized ArrayList<String> getUser(int pg) {
		ArrayList al = new ArrayList();
		try {
			String temp;
			URL url = new URL(
					"http://ren.baidu.com/uquery/?type=2&fn=0&word=&ln=10&sex1=0&sex2=0&sex3=0&age_from=18&age_to=26&marry=0&blood=0&zodiac=0&space_ok=0&baiduhi_ok=0&vocation=0&education=0&birth_province=&birth_city=&birth_home=&birth_other=&reside_province=&reside_city=&reside_home=&reside_other=&tag_school=&tag_book=&tag_music=&tag_movie=&tag_sport=&tag_person=&tag_other=&tag_brand=&tag_unit=&&pn="
							+ pg);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream(), "gb2312"));

			while ((temp = in.readLine()) != null) {
				if (!(temp.trim()
						.startsWith("s.src=\"http://pdc.baidu.com/uinfo/?callback")))
					continue;
				String t = temp.trim().substring(7, 357);
				URL turl = new URL(t);
				BufferedReader tin = new BufferedReader(new InputStreamReader(
						turl.openStream(), "gb2312"));
				while ((temp = tin.readLine()) != null) {
					if (!(temp.trim().startsWith("enusername")))
						continue;
					al.add(temp.trim().substring(12, temp.trim().length() - 2));
				}

				tin.close();
			}

			in.close();
		} catch (Exception e) {
			return null;
		}

		return al;
	}
}